package com.gz.controller;

import com.gz.entity.Monster;
import com.gz.service.MonsterService;
import com.gz.service.impl.MonsterServiceImpl;
import com.gz.springmvc.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@Controller
public class MonsterHandler {

    @AutoWired
    private MonsterService monsterService;

    @RequestMapping(value = "/monster/list")
    public String listMonsters(HttpServletRequest request,
                             HttpServletResponse response,
                             @RequestParam("name")String name) throws ServletException, IOException {
        //这里我也写了没有@RequestParam的情况即默认情况下
        //根据参数名匹配
        System.out.println("name= "+name);
        return "forward:/test/ok.jsp";
    }

    //编写方法，返回json格式数据
    @RequestMapping(value = "/monster/list/json")
    @ResponseBody //表示以json格式返回
    public ArrayList<Monster> listMonsterByJson(HttpServletRequest request,
                                       HttpServletResponse response) throws ServletException, IOException {
        Monster monster1 = new Monster(1, "gz",10,"nl");
        Monster monster2 = new Monster(2, "xx",12,"kk");
        ArrayList<Monster> list=new ArrayList<>();
        list.add(monster1);
        list.add(monster2);
        return list;
    }
}
